-
Notifications
You must be signed in to change notification settings - Fork 0
/
zad2.py
90 lines (68 loc) · 1.9 KB
/
zad2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
ZŁOŻONOŚĆ:
Obliczeniowa: O(D)
Pamięciowa: O(D)
"""
from zad2testy import runtests
class Node:
def __init__(self):
self.is_prefix = False
self.left = None
self.right = None
self.parent = None
class PrefixTree:
def __init__(self):
self.root = Node()
def insert(self, string):
curr = self.root
for c in string:
if c == '0':
if curr.left:
curr.left.is_prefix = True
else:
curr.left = Node()
curr.left.parent = curr
curr = curr.left
else:
if curr.right:
curr.right.is_prefix = True
else:
curr.right = Node()
curr.right.parent = curr
curr = curr.right
def get_prefixes(self):
leaves = self.get_prefix_leaves()
prefixes = []
for node in leaves:
prefixes.append(self.get_prefix(node))
return prefixes
def get_prefix_leaves(self):
leaves = []
def dfs(node):
is_leaf = True
if node.left and node.left.is_prefix:
is_leaf = False
dfs(node.left)
if node.right and node.right.is_prefix:
is_leaf = False
dfs(node.right)
if is_leaf:
leaves.append(node)
dfs(self.root)
return leaves
def get_prefix(self, node):
chars = []
while node.parent:
if node.parent.left is node:
chars.append('0')
else:
chars.append('1')
node = node.parent
chars.reverse()
return ''.join(chars)
def double_prefix(L):
pt = PrefixTree()
for s in L:
pt.insert(s)
return pt.get_prefixes()
runtests(double_prefix)